home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / SFILTER2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  7.2 KB  |  282 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* sfilter2.c */
  21. /* Additional stream functions for filters */
  22. #include <stdio.h>
  23. #include "memory_.h"
  24. #include "std.h"
  25. #include "scanchar.h"
  26. #include "stream.h"
  27.  
  28. /* Generic functions in sfilter.c */
  29. extern int s_filter_write_flush(P1(stream *));
  30. extern int s_filter_close(P1(stream *));
  31.  
  32. /* ------ ASCII85Encode ------ */
  33.  
  34. /* Flush the buffer */
  35. private int
  36. s_A85E_write_buf(register stream *s)
  37. {    register stream *strm = s->strm;
  38.     register byte *p = s->cbuf;
  39.     register int count = s->cptr + 1 - p;
  40.     while ( count >= 4 )
  41.       { ulong word =
  42.           ((ulong)(((uint)p[0] << 8) + p[1]) << 16) +
  43.           (((uint)p[2] << 8) + p[3]);
  44.         if ( word == 0 )
  45.           sputc(strm, 'z');
  46.         else
  47.           { ulong q = word / (85L*85*85*85);
  48.         ushort w1;
  49.         sputc(strm, q + '!');
  50.         word -= q * (85L*85*85*85);
  51.         q = word / (85L*85*85);
  52.         sputc(strm, q + '!');
  53.         word -= q * (85L*85*85);
  54.         q = word / (85*85);
  55.         sputc(strm, q + '!');
  56.         w1 = (ushort)(word - q * (85L*85));
  57.         sputc(strm, w1 / 85 + '!');
  58.         sputc(strm, w1 % 85 + '!');
  59.         if ( !(count & 60) )
  60.           sputc(strm, '\n');
  61.           }
  62.         count -= 4, p += 4;
  63.       }
  64.     memcpy(s->cbuf, p, count);
  65.     s->cptr = s->cbuf + count - 1;
  66.     return 0;
  67. }
  68.  
  69. /* Close the stream, flushing a partial word. */
  70. private int
  71. s_A85E_close(register stream *s)
  72. {    stream *strm = s->strm;
  73.     int count;
  74.     (*s->procs.write_buf)(s);
  75.     count = s->cptr - s->cbuf + 1;
  76.     if ( count > 0 )
  77.        {    /* Handle leftover bytes.  1 <= count <= 3. */
  78.         /* All the bytes are at the beginning of the buffer. */
  79.         byte ebuf[5];
  80.         stream sst;
  81.         s->cptr[1] = s->cptr[2] = s->cptr[3] = 0xff;
  82.         s->cptr = s->cbuf + 3;
  83.         swrite_string(&sst, ebuf, 5);
  84.         s->strm = &sst;
  85.         (*s->procs.write_buf)(s);    /* force out final codes */
  86.         sputs(strm, ebuf, count + 1);
  87.        }
  88.     sputs(strm, (byte *)"~>", 2);
  89.     return s_std_close(s);
  90. }
  91.  
  92. /* Stream procedures */
  93. const stream_procs s_A85E_procs =
  94.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_A85E_close,
  95.     NULL, s_A85E_write_buf
  96.    };
  97.  
  98. /* ------ ASCII85Decode ------ */
  99.  
  100. /* Refill the buffer */
  101. private int
  102. s_A85D_read_buf(register stream *s)
  103. {    register stream *strm = s->strm;
  104.     register byte *p = s->cbuf;
  105.     byte *limit = p + s->bsize - 4;
  106.     int ccount = 0;
  107.     ulong word = 0;
  108.     while ( p < limit )
  109.       { int ch = sgetc(strm);
  110.         uint ccode = ch - '!';
  111.         if ( ccode < 85 )        /* catches ch < '!' as well */
  112.           { word = word * 85 + ccode;
  113.         if ( ++ccount == 5 )
  114.          { p[0] = word >> 24;
  115.            p[1] = (byte)(word >> 16);
  116.            p[2] = (byte)((uint)word >> 8);
  117.            p[3] = (byte)word;
  118.            p += 4;
  119.            word = 0;
  120.            ccount = 0;
  121.          }
  122.           }
  123.         else if ( ch == 'z' && ccount == 0 )
  124.           p[0] = p[1] = p[2] = p[3] = 0,
  125.           p += 4;
  126.         else if ( scan_char_decoder[ch] == ctype_space )
  127.           ;
  128.         else if ( ch == '~' && sgetc(strm) == '>' )
  129.          { /* Handle odd bytes */
  130.            s->end_status = EOFC;
  131.            switch ( ccount )
  132.            {
  133.         case 0:
  134.             break;
  135.         case 1:            /* syntax error */
  136.             s->end_status = ERRC;
  137.             break;
  138.         case 2:            /* 1 odd byte */
  139.             word = word * (85L+85*85) + 0xffffffL;
  140.             goto o1;
  141.         case 3:            /* 2 odd bytes */
  142.             word = word * (85L*85) + 0xffffL;
  143.             goto o2;
  144.         case 4:            /* 3 odd bytes */
  145.             word = word * 85 + 0xffL;
  146.             p[2] = (byte)(word >> 8);
  147. o2:            p[1] = (byte)(word >> 16);
  148. o1:            p[0] = (byte)(word >> 24);
  149.             p += ccount - 1;
  150.            }
  151.            break;
  152.          }
  153.         else            /* syntax error or exception */
  154.          { s->end_status = (ch < 0 ? ch : ERRC);
  155.            break;
  156.          }
  157.       }
  158.     s->cptr = s->cbuf - 1;
  159.     s->endptr = p - 1;
  160.     return 0;
  161. }
  162.  
  163. /* Stream procedures */
  164. const stream_procs s_A85D_procs =
  165.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_filter_close,
  166.     s_A85D_read_buf, NULL
  167.    };
  168.  
  169. /* ------ RunLengthEncode ------ */
  170.  
  171. /* Initialize */
  172. void
  173. s_RLE_init(register stream *s, uint rec_size)
  174. {    s->record_size = (rec_size == 0 ? max_uint : rec_size);
  175.     s->record_left = s->record_size;
  176. }
  177.  
  178. /* Empty the buffer */
  179. private int
  180. s_RLE_write_buf(register stream *s)
  181. {    register stream *strm = s->strm;
  182.     register byte *p = s->cbuf;
  183.     while ( p <= s->cptr )
  184.        {    byte *beg = p, *q;
  185.         uint count = s->cptr - p + 1;
  186.         if ( count > s->record_left)
  187.             count = s->record_left;
  188.         if ( count > 127 )
  189.             count = 127;
  190.         q = p + count;
  191.         if ( count > 2 && p[1] == p[0] )
  192.            {    /* Recognize leading repeated byte */
  193.             do { p++; }
  194.             while ( p + 1 < q && p[1] == p[0] );
  195.             p++;
  196.             sputc(strm, 257 - (p - beg));
  197.             sputc(strm, *beg);
  198.            }
  199.         else
  200.            {    while ( p + 2 < q && (p[1] != p[0] || p[2] != p[0]) )
  201.                 p++;
  202.             if ( p + 2 >= q ) p = q;
  203.             sputc(strm, p - beg - 1);
  204.             sputs(strm, beg, p - beg);
  205.            }
  206.         s->record_left -= p - beg;
  207.         if ( s->record_left == 0 )
  208.             s->record_left = s->record_size;
  209.        }
  210.     s->cptr = s->cbuf - 1;
  211.     return 0;
  212. }
  213.  
  214. /* Close */
  215. private int
  216. s_RLE_close(register stream *s)
  217. {    (*s->procs.write_buf)(s);
  218.     sputc(s->strm, 128);
  219.     return s_std_close(s);
  220. }
  221.  
  222. /* Stream procedures */
  223. const stream_procs s_RLE_procs =
  224.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_RLE_close,
  225.     NULL, s_RLE_write_buf
  226.    };
  227.  
  228. /* ------ RunLengthDecode ------ */
  229.  
  230. /* Initialize */
  231. void
  232. s_RLD_init(register stream *s)
  233. {    s->odd = -1;
  234. }
  235.  
  236. /* Refill the buffer */
  237. private int
  238. s_RLD_read_buf(register stream *s)
  239. {    register stream *strm = s->strm;
  240.     register byte *p = s->cbuf;
  241.     byte *limit = p + s->bsize;
  242.     int b = s->odd;
  243.     if ( b < 0 ) b = sgetc(strm);
  244.     for ( ; ; )
  245.        {    uint count;
  246.         if ( b < 0 ) break;    /* EOF/ERR */
  247.         if ( b < 128 )
  248.            {    if ( b >= limit - p )    /* data won't fit */
  249.                 break;
  250.             count = sgets(strm, p, b + 1);
  251.             p += count;
  252.             b = -1;
  253.             if ( count == 0 && strm->end_status )    /* EOF/ERR */
  254.                 break;
  255.            }
  256.         else if ( b == 128 )    /* end of data */
  257.            {    s->end_status = EOFC;
  258.             b = -1;
  259.             break;
  260.            }
  261.         else if ( (count = 257 - b) > limit - p )
  262.             break;        /* won't fit */
  263.         else
  264.            {    b = sgetc(strm);
  265.             if ( b < 0 ) break;    /* EOF/ERR */
  266.             memset(p, b, count);
  267.             p += count;
  268.            }
  269.         b = sgetc(strm);
  270.        }
  271.     s->cptr = s->cbuf - 1;
  272.     s->endptr = p - 1;
  273.     s->odd = b;
  274.     return 0;
  275. }
  276.  
  277. /* Stream procedures */
  278. const stream_procs s_RLD_procs =
  279.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_std_close,
  280.     s_RLD_read_buf, NULL
  281.    };
  282.